<template>
  <el-form label-position="top">
    <el-form-item 
      v-for="childComp in component.childComponents" 
      :key="childComp.name" 
      :label="childComp.label || childComp.name"
    >
      <div class="child-component-config">
        <!-- 子组件添加按钮 -->
        <el-button 
          type="primary" 
          size="small" 
          @click="$emit('add-child', {
            childName: childComp.name,
            childComp: childComp
          })"
          class="mb-2"
        >
          添加{{childComp.label || childComp.name}}
        </el-button>

        <!-- 子组件属性配置 -->
        <div v-if="componentChildrenModel[childComp.name] && childComp.props">
          <h4>属性配置</h4>
          <div 
            v-for="(prop, propName) in childComp.props" 
            :key="propName" 
            class="config-item sub-config-item"
          >
            <div class="config-label">
              <span>{{ prop.label || propName }}</span>
              <el-tooltip v-if="prop.description" :content="prop.description" placement="top">
                <el-icon><QuestionFilled /></el-icon>
              </el-tooltip>
            </div>
            <el-input
              v-model="componentChildrenModel[childComp.name].props[propName]"
              :placeholder="`输入${prop.label || propName}`"
            />
          </div>
        </div>

        <!-- 子组件的插槽配置 -->
        <div v-if="childComp.slots && componentChildrenModel[childComp.name]">
          <h4>插槽配置</h4>
          <div 
            v-for="(slot, slotName) in childComp.slots" 
            :key="slotName" 
            class="config-item sub-config-item"
          >
            <div class="config-label">
              <span>{{ slotName }}</span>
              <el-tooltip v-if="slot.description" :content="slot.description" placement="top">
                <el-icon><QuestionFilled /></el-icon>
              </el-tooltip>
            </div>
            
            <el-radio-group v-model="componentChildrenModel[childComp.name].slots[slotName].isComponent" class="mb-2">
              <el-radio :label="false">文本</el-radio>
              <el-radio :label="true">组件</el-radio>
            </el-radio-group>
            
            <div v-if="componentChildrenModel[childComp.name].slots[slotName].isComponent">
              <el-select 
                v-model="componentChildrenModel[childComp.name].slots[slotName].content" 
                placeholder="选择组件" 
                class="w-full"
              >
                <el-option
                  v-for="comp in availableSlotComponents"
                  :key="comp.name"
                  :label="comp.label"
                  :value="comp.name"
                />
              </el-select>
            </div>
            
            <el-input
              v-else
              v-model="componentChildrenModel[childComp.name].slots[slotName].content"
              :placeholder="`输入${slotName}插槽内容`"
              type="textarea"
              :rows="2"
            />
          </div>
        </div>
      </div>
    </el-form-item>
  </el-form>
</template>

<script>
import { defineComponent, computed } from 'vue'
import { 
  ElForm,
  ElFormItem,
  ElButton,
  ElTooltip,
  ElIcon,
  ElInput,
  ElRadioGroup,
  ElRadio,
  ElSelect,
  ElOption,
  ElHeader,
  ElAside,
  ElMain,
  ElFooter,
  ElContainer
} from 'element-plus'
import { QuestionFilled } from '@element-plus/icons-vue'

export default defineComponent({
  name: 'ChildrenConfig',
  components: {
    QuestionFilled,
    ElForm,
    ElFormItem,
    ElButton,
    ElTooltip,
    ElIcon,
    ElInput,
    ElRadioGroup,
    ElRadio,
    ElSelect,
    ElOption,
    ElHeader,
    ElAside,
    ElMain,
    ElFooter,
    ElContainer
  },
  props: {
    component: {
      type: Object,
      required: true
    },
    componentChildren: {
      type: Object,
      required: true
    },
    availableSlotComponents: {
      type: Array,
      required: true
    }
  },
  emits: ['add-child'],
  setup(props, { emit }) {
   
    // 使用计算属性创建双向绑定
    const componentChildrenModel = computed({
      get: () => props.component.childComponents,
      set: (value) => emit('update:componentChildren', value)
    })

    return {
      componentChildrenModel
    }
  }
})
</script>

<style scoped>
.child-component-config {
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 10px;
  margin-bottom: 10px;
}

.sub-config-item {
  margin-left: 10px;
  padding-left: 10px;
  border-left: 2px solid #eee;
  margin-bottom: 15px;
}

.config-label {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

.config-label span {
  margin-right: 5px;
  font-weight: 500;
}

.mb-2 {
  margin-bottom: 8px;
}

.w-full {
  width: 100%;
}

h4 {
  margin-top: 0;
  margin-bottom: 10px;
  font-size: 14px;
  font-weight: bold;
}
</style>